package com.hunter.api.http;

import javax.servlet.http.HttpServletRequest;

import com.alibaba.fastjson.JSONObject;

import com.hunter.api.bean.SqlConfigBean;
import com.hunter.api.handler.SqlHandler;
import com.hunter.api.handler.annotation.UserLoginToken;
import com.hunter.api.help.result.ErrorHelp;
import com.hunter.api.help.UserHelper;
import com.hunter.api.mapper.SqlMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @version 1.0
 * @Author : Hunter.huang
 * @ClassName SqlHttp
 * @Date: 2019/11/21 9:46
 * Description:
 */
@Controller
@Slf4j
@CrossOrigin(origins = "*",maxAge = 3600)
@Transactional(propagation = Propagation.REQUIRED)
public class SqlHttp {

    @Autowired
    private SqlHandler sqlHandler;

    @Autowired
    UserHelper userHelper;

    @Autowired
    private SqlMapper sqlMapper;

    @ResponseBody
    @UserLoginToken
    @RequestMapping(value ="/qry/all")
    public JSONObject queryAll(@RequestBody SqlConfigBean sqlConfigBean, HttpServletRequest httpServletRequest){
        JSONObject json = new JSONObject();
        try {
            sqlConfigBean.setData(this.userHelper.getUserInfo(httpServletRequest, sqlConfigBean.getData()));
            JSONObject data = sqlConfigBean.getData();
            sqlConfigBean = sqlMapper.SqlConfigInfo(sqlConfigBean);
            if (sqlConfigBean != null) {
                JSONObject config_json = JSONObject.parseObject(sqlConfigBean.getConfig_json());
                Integer is_limit = sqlConfigBean.getIs_limit();
                JSONObject futQuery = sqlHandler.queryAll(config_json,data,is_limit);
                json.put("code", 20000);
                json.put("msg", "查询成功");
                json.put("data", futQuery);
            } else {
                json = ErrorHelp.SQL_NOT_EXISTS;
            }
        }catch (Exception e){
            json.put("code", 500);
            json.put("msg", e.getMessage());
            json.put("data","{}" );
        }finally {
            return json;
        }

    }

    @ResponseBody
    @UserLoginToken
    @RequestMapping(value ="/qry/row")
    public JSONObject queryRow(@RequestBody SqlConfigBean sqlConfigBean, HttpServletRequest httpServletRequest){
        JSONObject json = new JSONObject();
        try {
            sqlConfigBean.setData(this.userHelper.getUserInfo(httpServletRequest, sqlConfigBean.getData()));
            JSONObject data = sqlConfigBean.getData();
            sqlConfigBean = sqlMapper.SqlConfigInfo(sqlConfigBean);
            if (sqlConfigBean != null) {
                JSONObject config_json = JSONObject.parseObject(sqlConfigBean.getConfig_json());
                JSONObject futQuery = sqlHandler.queryRow(config_json,data);
                json.put("code", 20000);
                json.put("msg", "查询成功");
                json.put("data", futQuery);
            } else {
                json = ErrorHelp.SQL_NOT_EXISTS;
            }
        }catch (Exception e){
            json.put("code", 500);
            json.put("msg", e.getMessage());
            json.put("data","{}" );
        }finally {
            return json;
        }

    }

    @ResponseBody
    @UserLoginToken
    @RequestMapping(value ="/qry/scalar")
    public JSONObject queryScalar(@RequestBody SqlConfigBean sqlConfigBean, HttpServletRequest httpServletRequest){
        JSONObject json = new JSONObject();
        try {
            sqlConfigBean.setData(this.userHelper.getUserInfo(httpServletRequest, sqlConfigBean.getData()));
            JSONObject data = sqlConfigBean.getData();
            sqlConfigBean = sqlMapper.SqlConfigInfo(sqlConfigBean);
            if (sqlConfigBean != null) {
                JSONObject config_json = JSONObject.parseObject(sqlConfigBean.getConfig_json());
                JSONObject futQuery = sqlHandler.queryScalar(config_json,data);
                json.put("code", 20000);
                json.put("msg", "查询成功");
                json.put("data", futQuery);
            } else {
                json = ErrorHelp.SQL_NOT_EXISTS;
            }
        }catch (Exception e){
            e.printStackTrace();
            json.put("code", 500);
            json.put("msg", e.getMessage());
            json.put("data","{}" );
            return json;
        }
        return json;


    }

    @ResponseBody
    @UserLoginToken
    @RequestMapping(value ="/qry/exec")
    public JSONObject queryExec(@RequestBody SqlConfigBean sqlConfigBean, HttpServletRequest httpServletRequest){
        JSONObject json = new JSONObject();
        try {
            sqlConfigBean.setData(this.userHelper.getUserInfo(httpServletRequest, sqlConfigBean.getData()));
            JSONObject data = sqlConfigBean.getData();
            sqlConfigBean = sqlMapper.SqlConfigInfo(sqlConfigBean);
            if (sqlConfigBean != null) {
                JSONObject config_json = JSONObject.parseObject(sqlConfigBean.getConfig_json());
                JSONObject futQuery = sqlHandler.execSql(config_json,data,"update");
                json.put("code", 20000);
                json.put("msg", "查询成功");
                json.put("data", futQuery);
            } else {
                json = ErrorHelp.SQL_NOT_EXISTS;
            }
        }catch (Exception e){
            e.printStackTrace();
            json.put("code", 500);
            json.put("msg", e.getMessage());
            json.put("data","{}" );
            return json;
        }
        return json;

    }

}
